package com.qiaotongtianxia.loans.activity;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.support.annotation.Nullable;
import android.support.v4.content.ContextCompat;
import android.text.TextUtils;
import android.view.View;

import com.qiaotongtianxia.loans.R;
import com.qiaotongtianxia.loans.functionInterface.IBaseRequestImp;
import com.qiaotongtianxia.loans.net.Api;
import com.qiaotongtianxia.loans.util.ToastUtil;
import com.qiaotongtianxia.loans.views.BaseEditText;
import com.qiaotongtianxia.loans.views.BaseTextView;
import com.qiaotongtianxia.loans.views.ButtonObserver;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by lisen on 2017/11/21.
 *
 * @author lisen < 4533548588@qq.com >
 */

public class ForgetPWActivity extends BaseActivity {
    @BindView(R.id.tv_nav_title)
    BaseTextView tvNavTitle;
    @BindView(R.id.et_phone)
    BaseEditText etPhone;
    @BindView(R.id.et_verify)
    BaseEditText etVerify;
    @BindView(R.id.tv_verifyCode)
    BaseTextView tvVerifyCode;
    @BindView(R.id.et_newPassword)
    BaseEditText etNewPassword;
    @BindView(R.id.et_confirmPassword)
    BaseEditText etConfirmPassword;
    @BindView(R.id.btn_reset)
    ButtonObserver btn_reset;

    private CountDownTimer timer = new CountDownTimer(60000, 1000) {
        @SuppressLint("SetTextI18n")
        @Override
        public void onTick(long millisUntilFinished) {
            tvVerifyCode.setEnabled(false);
            tvVerifyCode.setTextColor(ContextCompat.getColor(ForgetPWActivity.this,R.color.gray));
            tvVerifyCode.setText(millisUntilFinished / 1000 +"s");
        }

        @Override
        public void onFinish() {
            tvVerifyCode.setTextColor(ContextCompat.getColor(ForgetPWActivity.this,R.color.mainColor));
            tvVerifyCode.setText("获取验证码");
            tvVerifyCode.setEnabled(true);
        }
    };

    @Override
    public int getLayoutId() {
        return R.layout.activity_forget_pw;
    }

    @Override
    public void onBaseCreate(@Nullable Bundle savedInstanceState) {
        btn_reset.observers(etPhone,etVerify,etNewPassword,etConfirmPassword);
    }

    @Override
    public Api onApiCreate() {
        return new Api(this);
    }

    @OnClick({R.id.iv_nav_back, R.id.tv_verifyCode, R.id.btn_reset})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.iv_nav_back:
                finish();
                break;
            case R.id.tv_verifyCode:
                String phone = etPhone.getText().toString();
                if (TextUtils.isEmpty(phone)){
                    ToastUtil.showShort(this,getString(R.string.inputPhone));
                    return;
                }
                api.verifyCode(phone, new IBaseRequestImp<String>() {
                    @Override
                    public void onRequestSuccess(String s) {
                        timer.start();
                    }
                });
                break;
            case R.id.btn_reset:
                String password = etNewPassword.getText().toString();
                String confimPw = etConfirmPassword.getText().toString();
                if (password.contains(" ")){
                    ToastUtil.showShort(this,"密码不能包含空格");
                    return;
                }
                if (password.length() < 6 || password.length() > 16){
                    ToastUtil.showShort(this,getString(R.string.passwordHint));
                    return;
                }
                if (!password.equals(confimPw)){
                    ToastUtil.showShort(this,getString(R.string.confirmPwError));
                    return;
                }
                final String p = etPhone.getText().toString();
                String code = etVerify.getText().toString();
                api.forgetPW(p, password, confimPw, code, new IBaseRequestImp<Nullable>() {
                    @Override
                    public void onRequestSuccess(Nullable nullable) {
                        ToastUtil.showShort(ForgetPWActivity.this,"修改成功");
                        finish();
                    }
                });

                break;
        }
    }

    @Override
    public void initTitle() {
        tvNavTitle.setText(getString(R.string.forgetPW));
    }
    @Override
    protected void onDestroy() {
        if (timer != null){
            timer.cancel();
        }
        super.onDestroy();
    }
}
